package com.muyu.common.core.web.page;

import com.muyu.common.core.utils.StringUtils;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

import static com.muyu.common.core.constant.SplitConstants.EMPTY_STR;

/**
 * 分页数据
 *
 * @author muyu
 */
@Data
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
public class PageReq {
    /**
     * 当前记录起始索引
     */
    @Schema(title = "pageNum", name = "当前页", description = "当前记录起始索引", defaultValue = "1")
    private Integer pageNum;

    /**
     * 每页显示记录数
     */
    @Schema(title = "pageSize", name = "分页条数", description = "每页显示记录数", defaultValue = "10")
    private Integer pageSize;

    /**
     * 排序列
     */
    @Schema(title = "orderByColumn", name = "排序列", description = "列表查询排序列")
    private String orderByColumn;

    /**
     * 排序的方向desc或者asc
     */
    @Schema(title = "isAsc", name = "排序方向", description = "排序的方向desc或者asc", defaultValue = "asc")
    private String isAsc = "asc";

    /**
     * 分页参数合理化
     */
    @Schema(title = "reasonable", name = "合理分页参数", description = "分页参数合理化")
    private Boolean reasonable = true;

    public String getOrderBy () {
        if (StringUtils.isEmpty(orderByColumn)) {
            return "";
        }
        return StringUtils.toUnderScoreCase(orderByColumn) + EMPTY_STR + isAsc;
    }

    public void setIsAsc (String isAsc) {
        if (StringUtils.isNotEmpty(isAsc)) {
            // 兼容前端排序类型
            if ("ascending".equals(isAsc)) {
                isAsc = "asc";
            } else if ("descending".equals(isAsc)) {
                isAsc = "desc";
            }
            this.isAsc = isAsc;
        }
    }

    public Boolean getReasonable () {
        if (StringUtils.isNull(reasonable)) {
            return Boolean.TRUE;
        }
        return reasonable;
    }

}
